<?php
class ControllerModuleTellfriend extends Controller {
	private $error = array();
	
	public function index() {	
		//$this->document->title = 'Tip en ven';
		$this->data['template'] = $this->config->get('config_template');  

    	$this->data['action'] = HTTPS_SERVER . 'index.php?route=module/tellfriend';		
		$this->data['send'] = HTTPS_SERVER . 'index.php?route=module/tellfriend/sendToFriend';	
		
		$this->id = 'tellfriend';
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/tellfriend.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/tellfriend.tpl';
		} else {
			$this->template = 'default/template/module/tellfriend.tpl';
		}
		
		$this->render();
	}
	  
  	public function sendToFriend() {			
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()){
			$result = 'Din besked er sendt til en ven!';
			
			//$subject = 'Tip Event';
			$subject = 'Tjek dette ud!';
			
			if(!isset($this->request->post['message'])){
				$message = 'Kære!<br />Din ven har sendt til dig et link. Følg venligst dette link for at se flere detaljer.<br /><br />';            
				$message .= '<b><a href="' . $this->request->post['url'] . '">' . $this->request->post['url'] . '</a></b>';	            
			}
			else{
				$message = $this->request->post['message'];
	        	$link = $this->request->post['url']; //$this->request->post['link'];
	        	$replace = '<b><a href="' . $link . '">' . $link . '</a></b>';
	    		$message = str_replace($link, $replace, $message);  					
			}
			$message .= '<br /><br /><br />';
			$message .= '<b><a href="' . $this->config->get('config_url') . '">' . $this->config->get('config_name') . '</a></b><br />';
			$message .= str_replace("\n", '<br />', $this->config->get('config_address')) . '<br />';
			$message .= 'Tel: ' . $this->config->get('config_telephone') . '<br />';
			$message .= 'Fax: '  . $this->config->get('config_fax') . '<br />';
			$message .= 'Email: ' . $this->config->get('config_email');
				
			$this->sendMail($this->request->post['email_from'], $this->request->post['email_to'], $subject, $message);
		}
		else{
			$result = $this->error['email'];
		}
		
		echo $result;			
  	}
  	
  	private function validate() {
  		$pattern = '/^[A-Z0-9._%-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i';
		$error = false;
    	if (!preg_match($pattern, $this->request->post['email_from'])) {
      		$this->error['email'] = 'Indtast venligst en gyldig e-mail';
      		$error = true;
    	}

    	if (!preg_match($pattern, $this->request->post['email_to'])) {
      		$this->error['email'] = 'Indtast venligst en gyldig e-mail';
      		$error = true;
    	}
    	     
    	if (!$error) {     
      		return true;     
    	} 
    	else {
      		return false;
    	}
  	}
  	
  	private function sendMail($from, $to, $subject, $message){
		$mail = new Mail();
		$mail->protocol = $this->config->get('config_mail_protocol');
		$mail->parameter = $this->config->get('config_mail_parameter');
		$mail->hostname = $this->config->get('config_smtp_host');
		$mail->username = $this->config->get('config_smtp_username');
		$mail->password = $this->config->get('config_smtp_password');
		$mail->port = $this->config->get('config_smtp_port');
		$mail->timeout = $this->config->get('config_smtp_timeout');		
	  	$mail->setFrom($from);
	  	$mail->setTo($to);
	  	$mail->setSender($from);
	  	$mail->setSubject($subject);
	  	$mail->setHtml($message); 
      	$mail->send();	
	} 
}
?>                   